﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

/*********************************************************
 * 
 * programmer:		patrick.woolhoo@gmail.com
 * svn:				http://woolhoo-net.googlecode.com/svn/trunk/wnet/
 * class:			Pager
 * desc:			实现数据分页功能，和Model配合使用最佳
 * 
 * *******************************************************/

namespace Patrick.Custom {
	/// <summary>
	/// Pager 的摘要说明
	/// </summary>
	public class Pager {
		public enum Pages { First, Prev, Next, Last };

		private int _First = 0;
		private int _Prev = 0;
		private int _Next = 0;
		private int _Last = 0;
		private int _RecordCount = 0;
		private int _PageCount = 0;

		public int First {
			get { return _First; }
		}

		public int Prev {
			get { return _Prev; }
		}

		public int Next {
			get { return _Next; }
		}

		public int Last {
			get { return _Last; }
		}

		public int RecordCount {
			get { return _RecordCount; }
		}

		public int PageCount {
			get { return _PageCount; }
		}

		public Pager(int PageSize, int PageNo, string Table, string Filter) {
			Model m = new Model(Table);

			_RecordCount = m.Count(Filter);
			_PageCount = (int)Math.Ceiling(((double)_RecordCount / (double)PageSize));

			if (_RecordCount > 0) {
				if (PageNo != 1) _First = 1;
				else _First = 0;

				if (PageNo > 1) {
					if (PageNo <= _PageCount) _Prev = PageNo - 1;
					else _Prev = _PageCount - 1 > 0 ? _PageCount - 1 : 1;
				} else _Prev = 0;

				if (PageNo < _PageCount) {
					if (PageNo > 0) _Next = PageNo + 1;
					else _Next = 1;
				} else _Next = 0;

				if (PageNo != _PageCount) _Last = _PageCount;
				else _Last = 0;
			}
		}

		public Pager(int PageSize, int PageNo, string SubQuery) {
			StringBuilder sql = new StringBuilder("SELECT COUNT(*) AS Count FROM (");
			sql.Append(SubQuery);
			sql.Append(") AS [T]");

			_RecordCount = Convert.ToInt32(Db.QueryScalar(sql.ToString()));
			_PageCount = (int)Math.Ceiling(((double)_RecordCount / (double)PageSize));

			if (_RecordCount > 0) {
				if (PageNo != 1) _First = 1;
				else _First = 0;

				if (PageNo > 1) {
					if (PageNo <= _PageCount) _Prev = PageNo - 1;
					else _Prev = _PageCount - 1 > 0 ? _PageCount - 1 : 1;
				} else _Prev = 0;

				if (PageNo < _PageCount) {
					if (PageNo > 0) _Next = PageNo + 1;
					else _Next = 1;
				} else _Next = 0;

				if (PageNo != _PageCount) _Last = _PageCount;
				else _Last = 0;
			}
		}

		/// <summary>
		/// 返回翻页元素，有效则返回a元素否则返回span元素
		/// </summary>
		/// <param name="Which"></param>
		/// <param name="ValidClass"></param>
		/// <param name="InvalidClass"></param>
		/// <returns></returns>
		public string GetElement(Pages Which, string Title, string UrlParams, string ValidClass, string InvalidClass) {
			StringBuilder RetValue = new StringBuilder();
			int Temp = 0;

			switch (Which) {
				case Pages.First:
					Temp = _First;
					break;
				case Pages.Prev:
					Temp = _Prev;
					break;
				case Pages.Next:
					Temp = _Next;
					break;
				case Pages.Last:
					Temp = _Last;
					break;
			}

			if (Temp == 0) {
				RetValue.Append("<span");
				if (!String.IsNullOrEmpty(InvalidClass)) RetValue.Append(" class=\"" + InvalidClass + "\"");
				RetValue.Append(">" + Title + "</span>");
			} else {
				RetValue.Append("<a href=\"" + HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"] + "?p=" + Temp.ToString());
				if (!String.IsNullOrEmpty(UrlParams)) RetValue.Append("&" + UrlParams);
				RetValue.Append("\"");
				if (!String.IsNullOrEmpty(ValidClass)) RetValue.Append(" class=\"" + ValidClass + "\"");
				RetValue.Append(">" + Title + "</a>");
			}
			return RetValue.ToString();
		}




		public string Show(int type, int pageNo, string url) {
			switch (type) {
				case 1:
					return this.Show1(url);
				case 2:
					return this.Show2(pageNo, url);
			}
			return "";
		}



		public string Show1(string url) {
			StringBuilder ret = new StringBuilder();
			ret.Append(this.GetElement(Pages.First, "首页", url, "", ""));
			ret.Append(this.GetElement(Pages.Prev, "上页", url, "", ""));
			ret.Append(this.GetElement(Pages.Next, "下页", url, "", ""));
			ret.Append(this.GetElement(Pages.Last, "尾页", url, "", ""));
			return ret.ToString();
		}





		public string Show2(int pageNo, string url) {
			StringBuilder ret = new StringBuilder();
			int start = 1;
			int end = 1;

			ret.Append(this.GetElement(Pages.First, "首页", url, "", ""));
			ret.Append(this.GetElement(Pages.Prev, "上页", url, "", ""));

			if (pageNo > 5) start = pageNo - 5; else start = 1;
			if (this.PageCount - pageNo > 5) end = pageNo + 5; else end = this.PageCount;

			for (int i = start; i <= end; i++) {
				if (i == pageNo) ret.Append("<span>" + i + "</span>");
				else ret.Append("<a href=\"" + HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"] + "?p=" + i.ToString() + "&" + url + "\">" + i.ToString() + "</a>");
			}

			ret.Append(this.GetElement(Pages.Next, "下页", url, "", ""));
			ret.Append(this.GetElement(Pages.Last, "尾页", url, "", ""));
			return ret.ToString();
		}


		public string Show3(string url) {
			StringBuilder ret = new StringBuilder();
			ret.Append(this.GetElement(Pages.First, "First", url, "", ""));
			ret.Append(this.GetElement(Pages.Prev, "Prev", url, "", ""));
			ret.Append(this.GetElement(Pages.Next, "Next", url, "", ""));
			ret.Append(this.GetElement(Pages.Last, "Last", url, "", ""));
			return ret.ToString();
		}

	}
}